<template>
  <div v-loading.fullscreen.lock="fullscreenLoading" class="app-container">
    <!-- <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分类名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >添加顶级分类</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="synchronous"
        >分类同步</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :default-expand-all="false"
    >
      <el-table-column prop="name" label="商品名称" align="center" />
      <el-table-column label="创建时间" align="center" prop="created_at" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleEdit(scope.row)"
          >增加子分类
          </el-button>
          <el-button
            type="primary"
            @click="handleEdit(scope.row)"
          >修改
          </el-button>
          <el-button
            type="danger"
            @click="handleDel(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分类同步 -->
    <synchronous :open.sync="open" />
    <!-- 新增 -->
    <add :open1.sync="open1" :title="title" />
  </div>
</template>
<script>
import { getCategory, delCategory } from '@/api/product'
import synchronous from './synchronous'
import add from './add'
export default {
  name: 'Index',
  components: {
    synchronous,
    add
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      open1: false,
      title: '',
      // 表格数据
      list: [],
      form: {},
      selectData: [],
      // 查询参数
      queryParams: {
        name: undefined
      },
      show: false,
      ids: [],
      fullscreenLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    synchronous() {
      this.open = true
    },
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      getCategory().then(response => {
        if (response.code === 10000) {
          that.list = JSON.parse(JSON.stringify(response.data))
          that.loading = false
          that.selectData = JSON.parse(JSON.stringify(response.data))
          that.selectList(that.selectData)
        }
      })
    },
    /* 处理数据 */
    selectList(data) {
      const replaceKey = function(data) {
        data.forEach(function(row, index, arr) {
          const temp = { label: row.name, value: row.id }
          if (row.children !== undefined) {
            if (row.children.length > 0) {
              temp['children'] = row.children
            }
            arr[index] = temp
            if (arr[index].children.length > 0) {
              replaceKey(arr[index].children)
            }
          } else {
            arr[index] = temp
          }
        })
      }
      replaceKey(data)
      this.selectData.unshift({ label: '顶级仓库', value: 0 })
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.form = {}
      this.open1 = true
      this.title = '添加顶级分类'
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      // console.log(row)
      this.form = {
        id: row.id,
        ids: row.id,
        name: row.name,
        isstatus: row.isstatus,
        parent_id: row.parent_id
      }
      this.open = true
      this.title = '编辑'
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const that = this
      that.$confirm('是否删除该商品,如果该商品底下还有商品则一起删除,确定吗？？？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        // eslint-disable-next-line no-undef
        delCategory(row.id).then(response => {
          that.loading = false
          console.log(response)
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        name: undefined
      }
      this.handleQuery()
    }
  }
}
</script>

